
use anyhow;

use kube::client::ConfigExt;
use kube::{
    self,
    Client, Config,
};
//初始化k8s客户端
pub async fn init_k8s_client() ->Result<Client,anyhow::Error> {
    //获取本地 kubeconfig
    let mut config = Config::infer().await?;

    //不验证tls证书
    config.accept_invalid_certs = true;
    //tracing::info!("config: {:?}", &config);

    let https = config.rustls_https_connector().unwrap();


    let service = tower::ServiceBuilder::new()
        //kube-api-server地址
        .layer(
            tower::ServiceBuilder::new()
                .layer(config.base_uri_layer())
                .into_inner(),
        )
        //token验证（使用 kubeconfug）
        .option_layer(config.auth_layer()?)
        .service(hyper::Client::builder().build(https));
    //客户端
    let client = Client::new(service, config.default_namespace.clone());

    Ok(client)
}
